package com.mz.ozy.mvp.presenter;

import android.content.Context;

import com.mz.ozy.base.BasePresenterImpl;
import com.mz.ozy.bean.AddressListBean;
import com.mz.ozy.mvp.model.AddressModel;
import com.mz.ozy.mvp.view.AddressContract;
import com.mz.ozy.net.MyObserver;
import com.mz.ozy.utils.RxUtil;

import org.json.JSONObject;

import okhttp3.ResponseBody;

/**
 * Created by administrator on 2018/12/3.
 */
public class AddressPresenter extends BasePresenterImpl<AddressContract.View> implements AddressContract.Presenter {

    private AddressModel mAddressModel;

    public AddressPresenter() {
        mAddressModel = new AddressModel();
    }

    @Override
    public void onList(Context context) {
        mAddressModel.onList(context)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<AddressListBean>(mView) {

                    @Override
                    public void onSuccesss(AddressListBean bean) {
                        mView.onListSuccess(bean);
                    }
                });
    }

    @Override
    public void onAdd(Context context, String shuname, String shmobile, String shaddr,
                      String area0id, String area1id, String area2id, String isdefault) {
        mAddressModel.onAdd(context, shuname, shmobile, shaddr, area0id, area1id, area2id, isdefault)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView) {

                    @Override
                    public void onSuccesss(JSONObject jsonObject) {
                        mView.onAddSuccess();
                    }
                });
    }

    @Override
    public void onUpdate(Context context, String aid, String shuname, String shmobile, String shaddr,
                         String area0id, String area1id, String area2id) {
        mAddressModel.onUpdate(context, aid, shuname, shmobile, shaddr, area0id, area1id, area2id)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView) {

                    @Override
                    public void onSuccesss(JSONObject jsonObject) {
                        mView.onUpdateSuccess();
                    }
                });
    }

    @Override
    public void onDefault(Context context, String aid) {
        mAddressModel.onDefault(context, aid)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView) {

                    @Override
                    public void onSuccesss(JSONObject jsonObject) {
                        mView.onDefaultSuccess();
                    }
                });
    }

    @Override
    public void onDel(Context context, String aid) {
        mAddressModel.onDel(context, aid)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView) {

                    @Override
                    public void onSuccesss(JSONObject jsonObject) {
                        mView.onDelSuccess();
                    }
                });
    }
}
